//
//  WSQBaseTabBarViewController.m
//  WSQOCTools
//
//  Created by 翁胜琼 on 2018/8/28.
//  Copyright © 2018年 Dev John. All rights reserved.
//

#import "WSQBaseTabBarViewController.h"
#import "WSQBaseNavigationController.h"
#import "WSQBaseTabbarModel.h"


@interface WSQBaseTabBarViewController ()

@property (nonatomic , copy , readwrite) NSArray<WSQBaseTabbarModel *> *models;

@end

@implementation WSQBaseTabBarViewController

- (instancetype)initWithModels:(NSArray<WSQBaseTabbarModel *> *)models
{
    self = [super init];
    if (self) {
        [self addChildViewControllerWithModels:models];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

// 添加子控制器
- (void)addChildViewControllerWithModels:(NSArray<WSQBaseTabbarModel *> *)models
{
    _models = [models copy];
    __weak typeof(self) weakSelf = self;
    [models enumerateObjectsUsingBlock:^(WSQBaseTabbarModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        __strong typeof(weakSelf) self = weakSelf;
        
        UIViewController *vc;
        
        vc = [NSClassFromString(obj.vc) new];
        vc.navigationItem.title = obj.title;
        WSQBaseNavigationController *nav = [[WSQBaseNavigationController alloc] initWithRootViewController:vc];
        
        nav.tabBarItem.image = [[UIImage imageNamed:obj.normalImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        nav.tabBarItem.selectedImage = [[UIImage imageNamed:obj.selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        nav.tabBarItem.title = obj.title;
        
        [nav.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -3)];
        
        [self addChildViewController:nav];
    }];
}

- (void)setupUI
{
    self.tabBar.backgroundColor = [UIColor whiteColor];
//    self.tabBar.tintColor = UICOLORFROMHEX(0xFA8C16);
//    self.tabBar.shadowImage = [UIImage new];
//    self.tabBar.backgroundImage = [UIImage new];
    self.tabBar.translucent = false;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    
    return UIInterfaceOrientationPortrait;
}

// 设置状态栏样式为选中的子控制器样式
- (UIViewController *)childViewControllerForStatusBarStyle
{
    return self.selectedViewController;
}

// 旋屏支持
- (BOOL)shouldAutorotate
{
    return self.selectedViewController.shouldAutorotate;
}

- (void)dealloc {
    
    NSLog(@"Runing %@ %@",[self class], NSStringFromSelector(_cmd));
}

@end
